package com.whfc.fuum.redis.impl;

import com.whfc.common.util.JSONUtil;
import com.whfc.fuum.dto.SysDictDTO;
import com.whfc.fuum.redis.SysDictRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2020/8/13 15:31
 */
@Service
public class SysDictRedisDaoImpl implements SysDictRedisDao {

    private static final String SYS_DICT_KEY = "sys-dict-cache";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setDict(Integer id, SysDictDTO dictDTO) {
        String value = JSONUtil.toString(dictDTO);
        redisTemplate.opsForHash().put(SYS_DICT_KEY, String.valueOf(id), value);
    }

    @Override
    public SysDictDTO getDict(Integer id) {
        String val = String.valueOf(redisTemplate.opsForHash().get(SYS_DICT_KEY, String.valueOf(id)));
        return JSONUtil.parseObject(val, SysDictDTO.class);
    }

    @Override
    public void remove(Integer id) {
        redisTemplate.opsForHash().delete(SYS_DICT_KEY, String.valueOf(id));
    }

    @Override
    public boolean exists(Integer id) {
        return redisTemplate.opsForHash().hasKey(SYS_DICT_KEY, String.valueOf(id));
    }
}
